namespace JQuery.WebKit.Design
{
    using System;
    using System.Collections;
    using System.ComponentModel.Design;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI.Design;
    using System.Web.UI.Design.WebControls;
    using System.Windows.Forms;
    using JQuery.WebKit;

    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class WebGridDesigner : DataBoundControlDesigner
    {
        private void AddKeysAndBoundFields(IDataSourceViewSchema schema)
        {
            WebGridColumnCollection columns = ((WebGrid) base.Component).Columns;
            try
            {
                columns.Clear();
            }
            catch (Exception)
            {
            }
            if (schema != null)
            {
                IDataSourceFieldSchema[] fields = schema.GetFields();
                if ((fields != null) && (fields.Length > 0))
                {
                    new ArrayList();
                    foreach (IDataSourceFieldSchema schema2 in fields)
                    {
                        if (((WebGrid) base.Component).IsBindableType(schema2.DataType))
                        {
                            WebGridColumn column;
                            if ((schema2.DataType == typeof(bool)) || (schema2.DataType == typeof(bool?)))
                            {
                                column = new WebGridColumn();
                            }
                            else
                            {
                                column = new WebGridColumn();
                            }
                            string name = schema2.Name;
                            column.DataField = name;
                            column.PrimaryKey = schema2.PrimaryKey;
                            columns.Add(column);
                        }
                    }
                }
            }
        }

        private IDataSourceViewSchema GetDataSourceSchema()
        {
            DesignerDataSourceView designerView = base.DesignerView;
            if (designerView != null)
            {
                try
                {
                    return designerView.Schema;
                }
                catch (Exception)
                {
                    IComponentDesignerDebugService service = (IComponentDesignerDebugService) base.Component.Site.GetService(typeof(IComponentDesignerDebugService));
                    if (service != null)
                    {
                        service.Fail("DataSource_DebugService_FailedCall");
                    }
                }
            }
            return null;
        }

        public override string GetDesignTimeHtml()
        {
            return base.CreatePlaceHolderDesignTimeHtml();
        }

        protected override void OnSchemaRefreshed()
        {
            if (!base.InTemplateMode)
            {
                Cursor current = Cursor.Current;
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    ControlDesigner.InvokeTransactedChange(base.Component, new TransactedChangeCallback(this.SchemaRefreshedCallback), null, "Refresh Schema");
                    this.UpdateDesignTimeHtml();
                }
                finally
                {
                    Cursor.Current = current;
                }
            }
        }

        private bool SchemaRefreshedCallback(object context)
        {
            IDataSourceViewSchema dataSourceSchema = this.GetDataSourceSchema();
            if ((base.DataSourceID.Length > 0) && (dataSourceSchema != null))
            {
                this.AddKeysAndBoundFields(dataSourceSchema);
            }
            return true;
        }
    }
}

